﻿<%@ WebHandler Language="C#" Class="ExamInfo" %>

using System;
using System.Web;
using Newtonsoft.Json;
using System.Data;
using Common;
using System.Text;
using System.Collections;
using System.Collections.Generic;
using System.IO;

public class ExamInfo : IHttpHandler {
    
    public void ProcessRequest (HttpContext context) {
        context.Response.Charset = "UTF-8";
        context.Response.ContentType = "text/plain";


        string strResult = "";
        DataTable dtResult = new DataTable();
        string action = context.Request.QueryString["action"];
        string userid = context.Request.QueryString["userid"] != null ? context.Request.QueryString["userid"] : "";
        string username = context.Request.QueryString["username"] != null ? context.Request.QueryString["username"] : "";
        string paperid = context.Request.QueryString["paperid"] != null ? context.Request.QueryString["paperid"] : "";
        string ksjg = context.Request.QueryString["ksjg"] != null ? context.Request.QueryString["ksjg"] : "";
        string kssj = context.Request.QueryString["kssj"] != null ? context.Request.QueryString["kssj"] : "";
        string jssj = context.Request.QueryString["jssj"] != null ? context.Request.QueryString["jssj"] : "";
        string ksys = context.Request.QueryString["ksys"] != null ? context.Request.QueryString["ksys"] : "";
        switch (action)
        {
            case "submitexamscore":
                
                
                string strksjgdetail = PostInput(context.Request);
                try
                {
                    strksjgdetail = strksjgdetail.Substring(strksjgdetail.IndexOf('['));
                    strksjgdetail = strksjgdetail.Substring(0, strksjgdetail.IndexOf(']') + 1);
                }
                catch (Exception ec)
                {
                    strksjgdetail = ec.Message;
                }
                WriteToLog.SaveToLog("正常:"+strksjgdetail + "--"+ userid + "--" + username + "--" + paperid + "--" + ksjg + "--" + kssj + "--" + jssj);
                   
                List<uploadContext> listcon = JsonConvert.DeserializeObject<List<uploadContext>>(strksjgdetail);
                string sUploadResult = uploadSaveToDB(userid, username, paperid, ksjg, kssj, jssj,ksys, listcon);
                if (sUploadResult == "0")
                {
                    ReturnMsg rm = new ReturnMsg();
                    rm.retcode = 0;
                    rm.remark = "数据上传成功！";
                    rm.retdatatable = null;
                    rm.retjson = "";
                    strResult = rm.retdata;
                }
                else
                {
                    ReturnMsg rm = new ReturnMsg();
                    rm.retcode = 1;
                    rm.remark = "数据上传失败！";
                    rm.retdatatable = null;
                    rm.retjson = "[]";
                    strResult = rm.retdata;
                }
                break;
            case "searchexamresult":
                string strsql = "   select gh,xm,ksjg,a.paperBh,b.paperName,kssj,jssj from pe_llksjg a left join pe_paperMa b on a.paperBh=b.paperBh " +
                    " where gh='" + userid + "' " +
                    //" where a.paperBh='" + paperid + "' and gh='" + userid + "' " +
                    " order by ksls desc ";
                DataSet ds = new BLL.T_Public().Query(strsql);
                if (ds != null)
                    if (ds.Tables.Count > 0)
                        if (ds.Tables[0].Rows.Count > 0)
                        {
                            //for (int i = 0; i < ds.Tables[0].Rows.Count; i++)
                            //{
                            //    byte[] bShiT = (byte[])ds.Tables[0].Rows[i]["ShiT"];
                            //    string sShiT = Encoding.Default.GetString(bShiT);
                            //    ds.Tables[0].Rows[i]["ShiTi"] = sShiT;
                            //}
                            dtResult = ds.Tables[0];
                        }
                if (dtResult.Rows.Count > 0)
                {
                    ReturnMsg rm = new ReturnMsg();
                    rm.retcode = 0;
                    rm.remark = "获取成功！";
                    rm.retdatatable = dtResult;
                    rm.retjson = "";
                    strResult = rm.retdata;
                }
                else
                {
                    ReturnMsg rm = new ReturnMsg();
                    rm.retcode = 1;
                    rm.remark = "数据不存在！";
                    rm.retdatatable = null;
                    rm.retjson = "[]";
                    strResult = rm.retdata;
                }
                break;
        }


        context.Response.Write(strResult);
        context.Response.End();
        
        
    }

    public static string PostInput(HttpRequest request)
    {
        string reqStr = string.Empty;
        using (Stream s = request.InputStream)
        {
            using (StreamReader reader = new StreamReader(s, Encoding.UTF8))
            {
                reqStr = reader.ReadToEnd();
            }
            WriteToLog.SaveToLog(reqStr);
            reqStr = HttpUtility.UrlDecode(reqStr);
        }

        return reqStr;
    }
    public static string PostInputASC(HttpRequest request)
    {
        string reqStr = string.Empty;
        using (Stream s = request.InputStream)
        {
            using (StreamReader reader = new StreamReader(s, Encoding.ASCII))
            {
                reqStr = reader.ReadToEnd();
            }
            reqStr = HttpUtility.UrlDecode(reqStr);
        }

        return reqStr;
    }

    public static string PostInputUnicode(HttpRequest request)
    {
        string reqStr = string.Empty;
        using (Stream s = request.InputStream)
        {
            using (StreamReader reader = new StreamReader(s, Encoding.Unicode))
            {
                reqStr = reader.ReadToEnd();
            }
        }

        return reqStr;
    }
    
    
    public bool IsReusable {
        get {
            return false;
        }
    }

    public class uploadContext
    {
        public int stbh { get; set; }
        public string Right { get; set; }
        public string ksAnswer { get; set; }
        public string fs { get; set; }
        public string TiX { get; set; }
    }


    private string uploadSaveToDB(string userid,string xm,string paperid,string ksjg,string kssj,string jssj,string ksys, List<uploadContext> listUpload)
    {
        string sksjgls = DateTime.Now.ToString("yyyyMMddHHmmssff");
        try
        {
            for (int i = 0; i < listUpload.Count; i++)
            {
                Model.pe_llksjginfo mllksjginfo = new Model.pe_llksjginfo();
                
                string sTiX = "0";
                if (listUpload[i].TiX == "判断题")
                    sTiX = "0";
                else if (listUpload[i].TiX == "选择题")
                    sTiX = "1";
                else if (listUpload[i].TiX == "填空题")
                    sTiX = "2";
                else if (listUpload[i].TiX == "简答题")
                    sTiX = "3";
                else if (listUpload[i].TiX == "综合题")
                    sTiX = "4";
                
                mllksjginfo.TiX = sTiX;
                mllksjginfo.ksls = sksjgls;
                mllksjginfo.stbh = listUpload[i].stbh;
                mllksjginfo.RightAnswer = listUpload[i].Right;
                mllksjginfo.ksAnswer = listUpload[i].ksAnswer;
                mllksjginfo.fs = listUpload[i].fs;
                mllksjginfo.csn = sksjgls;
                mllksjginfo.addDateTime = DateTime.Now;

                new BLL.pe_llksjginfo().Add(mllksjginfo);
            }
            Model.pe_llksjg mllksjg = new Model.pe_llksjg();
            mllksjg.ksls = sksjgls;
            mllksjg.gh = userid;
            mllksjg.xm = xm;
            mllksjg.paperBh = paperid;
            mllksjg.ksjg = ksjg;
            mllksjg.kssj = kssj;
            mllksjg.jssj = jssj;
            mllksjg.ksys = ksys;
            new BLL.pe_llksjg().Add(mllksjg);
            return "0";
        }
        catch (Exception exc)
        {
            return "1";
        }
    }
    
}